import{campAddInfo,campAddFacilities,campAddCampsite} from"../../api/camp"
import{base_url}from"../../utils/request"
// pages/preview/preview.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    base_url:base_url,
    showInfo: false,
    showFac: true,
    type:'',
    modalHide:true,
    pvd:{},//预览数据
    imgs1:''
  },
  async sendCampData(){
    wx.showLoading({
      title: '正在上传中',
      mask:true
    })
    const instance=getApp()
    const cd=instance.globalData.campData;
    const campInfo=await campAddInfo(cd.campInfo);//上传基本信息

    if(campInfo.code===200){
     // 8d1f29aa-b48e-4295-89e5-2fab4acb2f65
      console.log(campInfo.msg)
      const caf=await campAddFacilities({
        campId:campInfo.msg,
        facilityIds:cd.facilitiesCurrentData
      })
      
      const factor= cd.campsiteData.map(item=>{
        item.factor=item.factorId.join(",")
        item.campId=campInfo.msg
        return item
      })
      const cac=await campAddCampsite(factor)
     if(caf.code==200 && cac.code==200){
       wx.hideLoading()
       wx.showModal({
         title: '系统提示',
         content: '上传成功',
         showCancel:false,
         success: (res) => {
           if (res.confirm) {
             wx.switchTab({
               url: '../index/index.wxml',
             })
           }
         }
       })
     }else{
      wx.hideLoading()
      wx.showToast({
        title: '上传失败',
        mask:true,
        duration:2000
      })
     }

    }

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const pvd=getApp().globalData.campData
    this.setData({pvd,img1:pvd.campInfo.campImgs})
  },
  showCampInfo() {
    this.setData({
      showInfo: !this.data.showInfo
    })
  },


  hideShowFacilities() {
    this.setData({
      showFac: !this.data.showFac
    })
  },

  // 获取滚动条当前位置
  onPageScroll(e){
    // console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop(e){ // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },
  // 周边景点
  toNext(){
    this.setData({
      modalHide:false
    })
  },
  hideModal(){
    this.setData({
      modalHide:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      type:options.type
    })
  },


  

})